package com.doubao.doubao.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doubao.doubao.common.api.ApiResult;
import com.doubao.doubao.common.exception.ApiAsserts;
import com.doubao.doubao.mapper.BmsGroupTrueMapper;
import com.doubao.doubao.mapper.BmsNewGroupMapper;
import com.doubao.doubao.model.entity.BmsGroupTrue;
import com.doubao.doubao.model.entity.BmsNewGroup;
import com.doubao.doubao.model.vo.JoinGroupVO;
import com.doubao.doubao.service.IBmsGroupTrueervice;
import com.doubao.doubao.service.IBmsNewGroupervice;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/group")
public class BmsNewGroupController extends BaseController {

    @Resource
    private IBmsNewGroupervice bmsNewGroupService;
    @Resource
    BmsNewGroupMapper BmsNewGroupMapper;


    @RequestMapping(value = "/newGroup", method = RequestMethod.GET)
    public ApiResult<?> findPage(@RequestParam(value = "groupleader", defaultValue = "1")String groupLeader){
        List<Map<String, Object>> BookPage =  BmsNewGroupMapper.selectMaps(new LambdaQueryWrapper<BmsNewGroup>().eq(BmsNewGroup::getGroupLeader, groupLeader));
        return ApiResult.success(BookPage);
    }

    @GetMapping("/JoinGroup")
    public ApiResult<List<JoinGroupVO>> getJoinMenu(@RequestParam(value = "userid", defaultValue = "1") String userid) {
        List<JoinGroupVO> lstGroup = bmsNewGroupService.getJoinMenu(userid);
        return ApiResult.success(lstGroup);
    }

    @RequestMapping(value = "/GroupLeader", method = RequestMethod.GET)
    public ApiResult<BmsNewGroup> getGroupLeader(@RequestParam(value = "groupid", defaultValue = "1")String groupId) {
        BmsNewGroup lstjoinGroup = bmsNewGroupService.getGroupLeader(groupId);
        return ApiResult.success(lstjoinGroup);
    }

    @PostMapping("/addgroup")
    public ApiResult<BmsNewGroup> addGroup(@RequestBody BmsNewGroup bmsNewGroup) {
        BmsNewGroup res =  BmsNewGroupMapper.selectOne(Wrappers.<BmsNewGroup>lambdaQuery()
                .eq(BmsNewGroup::getGroupId,bmsNewGroup.getGroupId()));
        if(res!=null){
            return ApiResult.failed("群id已经存在了，请更换一个");
        }
        bmsNewGroupService.save(bmsNewGroup);
        return ApiResult.success(bmsNewGroup);
    }

    @PostMapping("/ChoseJoingroup")
    public ApiResult<BmsNewGroup> chessGroup(@RequestBody BmsNewGroup bmsNewGroup) {
        BmsNewGroup res1 = BmsNewGroupMapper.selectOne(Wrappers.<BmsNewGroup>lambdaQuery()
                .eq(BmsNewGroup::getGroupId, bmsNewGroup.getGroupId()));
        BmsNewGroup res2 = BmsNewGroupMapper.selectOne(Wrappers.<BmsNewGroup>lambdaQuery()
                .ne(BmsNewGroup::getGroupLeader, bmsNewGroup.getGroupLeader())
                .eq(BmsNewGroup::getGroupId, bmsNewGroup.getGroupId())
        );
        if (res1 == null) {
            return ApiResult.failed("群不存在。请换一个吧");
        }

        if (res2 == null) {
            return ApiResult.failed("你不能加入自己的组群");
        }
        return ApiResult.success(bmsNewGroup);
    }
}
